﻿
using System;
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;
using System.Data;
using System.Web.Security;
using Gift.DAL;
using Gift.Model;

namespace WebSite.cn.ashx
{
    public class addFudianHandler : IHttpHandler, IRequiresSessionState
    {
        /// <summary>
        /// 您将需要在您网站的 web.config 文件中配置此处理程序，
        /// 并向 IIS 注册此处理程序，然后才能进行使用。有关详细信息，
        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        readonly MemberAccount theMemberAccount = new MemberAccount();
        readonly DALCard theCard = new DALCard();
        readonly Member theMember = new Member();


        public bool IsReusable
        {
            // 如果无法为其他请求重用托管处理程序，则返回 false。
            // 如果按请求保留某些状态信息，则通常这将为 false。
            get { return true; }
        }
        /// <summary>
        /// 105-该卡还未生效
        /// 106-该卡已过了兑换有效日期
        /// 110-您使用的专区提货卡，请点击我要兑换，进行直接提货操作，您也可查看帮助手册
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
          
            BasePage thePage = new BasePage();

            if (thePage.LoginUserMember == null)
            {
                context.Response.Redirect("login.aspx");
                return;
            }
            if (context.Request["code"].ToLower() != context.Session["checkCode"].ToString())
            {
                context.Response.Write("1002");
                return;
            }
            DB_Card oCard;
            try
            {
                long.Parse(context.Request["cardcode"].Trim());
            }
            catch
            {
                context.Response.Write("100");
                return;
            }
            oCard = theCard.GetModel(long.Parse(context.Request["cardcode"].Trim()), context.Request["cardpwd"].Trim());
            if (oCard != null)
            {
                if (oCard.states == 0)
                {
                    context.Response.Write("101");
                    return;
                }
                else if (oCard.states == 2)
                {
                    context.Response.Write("102");
                    return;
                }
                else if (oCard.states == 3)
                {
                    context.Response.Write("103");
                    return;
                }
                else if (oCard.states == 4)
                {
                    context.Response.Write("104");
                    return;
                }
                else if (oCard.StartTime > DateTime.Now.Date)
                {
                    context.Response.Write("105");
                    return;
                }
                else if (oCard.lasttime < DateTime.Now.Date)
                {

                    context.Response.Write("106");
                    return;

                }
                if (oCard.UseWay == 1)
                {
                    context.Response.Write("110");
                    return;
                }
             
                  if (DateTime.Now.Date <= oCard.endtime)
                    {

                        if (oCard.states == 1)
                        {
                            theMemberAccount.ConvertToAccount(int.Parse(thePage.LoginUserMember.MemberId), oCard.cardid);
                            context.Response.Write("108");
                            return;

                        }
                        else //很抱歉，充值失败，请联系客服
                        {
                            context.Response.Write("107");
                            return;
                        }
                    }
                    else
                    {
                        context.Response.Write("106");
                        return;
                    }
     
            }
            else
            {
                context.Response.Write("100");
                return;
            }

        }

        #endregion
    }
}
